from flask import current_app, jsonify
from flask import render_template
from flask import request
from flask import session

from info import constants
from info import redis_store
from info.models import User, News, Category
from info.response_code import RET
from info.utils.common import user_login_data
from . import index_blu

@index_blu.route("/")
@user_login_data
def index():
    '''
    显示页面
    1.如果用户已经登陆，将当前登陆用户的数据传到模板中，供模板显示
    :return:
    '''
    #取到用户id
    user_id = session.get("user_id",None)
    user = None
    news_list = None
    #获取新闻分类数据
    categories = Category.query.all()
    #定义列表保存分类数据
    categories_dicts = []

    # for category in categories:
    #     #enumerate枚举
    #     categories_dicts.append(category.to_dict())

    for category in enumerate(categories):
        #enumerate枚举
        categories_dicts.append(category[1].to_dict())

    if user_id:
        #尝试查询用户的模型
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)
    #获取点击排行数据
    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS)
    except Exception as e:
        current_app.logger.error(e)

    click_news_list =[]
    for news in news_list if news_list else []:
        click_news_list.append(news.to_basic_dict())


    data = {
        "user":user.to_dict() if user else None,
        "click_news_list":click_news_list,
        "categories":categories_dicts,
    }

    return render_template("news/index.html",data = data)

@index_blu.route("/favicon.ico")
def favicon():
    return current_app.send_static_file("news/favicon.ico")

@index_blu.route("/newslist")
def get_news_list():
    """
    获取指定分类的新闻列表
    1.获取参数
    2.校验参数
    3.查询数据
    4.返回数据
    :return:
    """
    # 1.获取参数
    #新闻的分类id
    cid = request.args.get("cid","1")
    page = request.args.get("page","1")
    per_page = request.args.get("per_page","10")

    #2.校验参数
    try:
        page = int(page)
        cid = int(cid)
        per_page = int(per_page)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR,errmsg="参数")

    filters = []
    if cid != 1:#查询的不是最新的数据
        filters.append(News.category_id == cid)

    # 3.查询数据
    try:
        paginate = News.query.filter(*filters).order_by(News.create_time.desc()).paginate(page,per_page,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="数据查询错误")

    #取到当前页的数据
    news_model_list = paginate.items #模型对象列表
    total_page = paginate.pages
    current_page = paginate.page

    #将模型对象列表转换成字典列表
    news_dict_li = []
    for news in news_model_list:
        news_dict_li.append(news.to_basic_dict())

    data = {
        "total_page":total_page,
        "current_page":current_page,
        "news_dict_li":news_dict_li,
    }

    return  jsonify(errno=RET.OK,errmsg="OK",data =data)

